package general.dbClass;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;

import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.imaging.jpeg.JpegProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.exif.ExifIFD0Directory;
import com.drew.metadata.exif.ExifSubIFDDirectory;
import com.drew.metadata.jpeg.JpegDirectory;


public class DBPicInfo {
	private int 		nr;
	private String 		name;
	private int 		rate;
	private DBExif		exif;
	private ArrayList<DBCategory> 	categories;
	private ArrayList<DBKeyword> 	keywords;
	
	public DBPicInfo() {
		nr 			= 0;
		name 		= "";
		rate 		= 0;
		exif		= null;
		categories 	= null;
		keywords 	= null;
	}
	
	public DBPicInfo(int nr,String name,int rate,ArrayList<DBCategory> categories,ArrayList<DBKeyword> keywords) {
		this.nr 		= nr;
		this.name 		= name;
		this.rate 		= rate;
		this.categories = categories;
		this.keywords 	= keywords;
	}
	
	public DBPicInfo(String name,int rate,byte[] data,ArrayList<DBCategory> categories,ArrayList<DBKeyword> keywords) {
		this.nr			= -1;
		this.name 		= name;
		this.rate 		= rate;
		//this.data		= new DBPicData(nr,data);
		this.keywords	= keywords;
		this.categories = categories;

		try {
			this.setExif(data);
		} catch (JpegProcessingException e) {
			e.printStackTrace();
		}
	}

	public DBPicInfo(int nr, String name, int rate, DBExif exif,ArrayList<DBCategory> categories, ArrayList<DBKeyword> keywords) {
		this.nr 		= nr;
		this.name 		= name;
		this.rate 		= rate;
		this.exif 		= exif;
		this.categories = categories;
		this.keywords 	= keywords;
	}

	public int getNr() {
		return nr;
	}
	
	public String getName() {
		return name;
	}
	
	public int getRate() {
		return rate;
	}
	
	public DBExif getExif() {
		return exif;
	}

	/*
	public byte[] getData() {
		return data.getData();
	}
	
	public DBPicData getDataObj() {
		return data;
	}
	
	public int getSize() {
		return data.getSize();
	}
	
	public byte[] getThumb() {
		return thumb.getData();
	}
	
	public DBThumbData getThumbObj() {
		return thumb;
	}
	 */
	
	public ArrayList<DBCategory> getCategories() {
		return categories;
	}
	
	public ArrayList<DBKeyword> getKeywords() {
		return keywords;
	}

	
	public void setNr(int nr) {
		this.nr = nr;
		//this.data.setNr(nr);
		//this.thumb.setNr(nr);
	}
	
	public void setExif(DBExif exif) {
		this.exif = exif;
	}
	
	public void setName(String name) {
		this.name = name;
	//	this.thumb.setName(name);
	}
	
	public void setRate(int rate) {
		this.rate = rate;
	}
	
	/*
	public void setData(byte[] data) {
		this.data.setData(data);
		
		try {
			this.setThumb();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		try {
			this.setExif();
		} catch (JpegProcessingException e) {
			e.printStackTrace();
		}
	}
	*/
	
	public void setCategories(ArrayList<DBCategory> categories) {
		this.categories = categories;
	}
	
	public void setKeywords(ArrayList<DBKeyword> keywords) {
		this.keywords = keywords;
	}
	
	public void setExif(byte[] data) throws JpegProcessingException {
		InputStream is = new ByteArrayInputStream(data); 
		Metadata metadata = JpegMetadataReader.readMetadata(is);
		
		String imgHeight 		= "";
		String imgWidth			= "";
		String exposureTime		= "";
		String fnumber			= "";
		String exposureProgram	= "";
		String timeOriginal		= "";
		String timeDigitized	= "";
		String colorSpace		= "";
		String exifImgWidth		= "";
		String exifImgHeight	= "";
		String exposureMode		= "";
		String make				= "";
		String model			= "";
		String orientation		= "";
		String xResolution		= "";
		String yResolution		= "";
		
		
		//read exif information from image's inputstream
		Directory dic = metadata.getDirectory(JpegDirectory.class);
		if(dic!=null) {
			imgHeight 	= dic.getDescription(JpegDirectory.TAG_JPEG_IMAGE_HEIGHT);
			imgWidth	= dic.getDescription(JpegDirectory.TAG_JPEG_IMAGE_WIDTH);
		}

		dic = metadata.getDirectory(ExifSubIFDDirectory.class);
		if(dic !=null) {
			exposureTime	= dic.getDescription(ExifSubIFDDirectory.TAG_EXPOSURE_TIME);
			fnumber			= dic.getDescription(ExifSubIFDDirectory.TAG_FNUMBER);
			exposureProgram	= dic.getDescription(ExifSubIFDDirectory.TAG_EXPOSURE_PROGRAM);
			timeOriginal	= dic.getDescription(ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL);
			timeDigitized	= dic.getDescription(ExifSubIFDDirectory.TAG_DATETIME_DIGITIZED);
			colorSpace		= dic.getDescription(ExifSubIFDDirectory.TAG_COLOR_SPACE);
			exifImgWidth	= dic.getDescription(ExifSubIFDDirectory.TAG_EXIF_IMAGE_WIDTH);
			exifImgHeight	= dic.getDescription(ExifSubIFDDirectory.TAG_EXIF_IMAGE_HEIGHT);
			exposureMode	= dic.getDescription(ExifSubIFDDirectory.TAG_EXPOSURE_MODE);
		}

		dic = metadata.getDirectory(ExifIFD0Directory.class);
		if(dic != null) {
			make		= dic.getDescription(ExifIFD0Directory.TAG_MAKE);
			model		= dic.getDescription(ExifIFD0Directory.TAG_MODEL);
			orientation	= dic.getDescription(ExifIFD0Directory.TAG_ORIENTATION);
			xResolution	= dic.getDescription(ExifIFD0Directory.TAG_X_RESOLUTION);
			yResolution	= dic.getDescription(ExifIFD0Directory.TAG_Y_RESOLUTION);
		}
		
		exif = new DBExif(imgHeight,imgWidth,exposureTime,fnumber,exposureProgram,timeOriginal,timeDigitized,colorSpace,exifImgWidth,
							exifImgHeight,exposureMode,make,model,orientation,xResolution,yResolution);
	}

	/*
	private void setThumb() throws IOException {
		InputStream 			is 		= new ByteArrayInputStream(data.getData());
		ByteArrayOutputStream 	os 		= new ByteArrayOutputStream();
		BufferedImage 			inBuf 	= ImageIO.read(is);
		Image 					tmp 	= inBuf.getScaledInstance(160, 120, Image.SCALE_SMOOTH);
		BufferedImage			retBuf 	= new BufferedImage(160,120,BufferedImage.TYPE_INT_RGB);
		Graphics 				g 		= retBuf.getGraphics();
		g.drawImage(tmp,0,0,null);
		
		ImageIO.write(retBuf, "jpg", os);
		
		os.flush();
		thumb.setData(os.toByteArray());
		os.close();
	}
	*/
}
